package written;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 写个JAVA程序开启5个计数线程从0开始计数，当所有线程计数值到1000时输出一个HelloWorld字符串
 *
 * @author TANGYE
 * @date 2021-01-02 10:09
 **/
public class QuestionTwoNoCDL implements Runnable {
    private static final int MAX_VALUE = 1000;
    private static AtomicInteger num = new AtomicInteger(0);

    public void printHelloWorld(int maxValue, AtomicInteger num) throws InterruptedException {
        if (num.addAndGet(1) == maxValue) {
            System.out.println("num is:" + num);
            System.out.println("HelloWorld");
            Thread.sleep(1000);
        }
    }


    public static void main(String[] args) {
        ExecutorService executors = Executors.newFixedThreadPool(5);
        ExecutorService executor = new ThreadPoolExecutor(1, 2,
                10000L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        executors.submit(new QuestionTwoNoCDL());
        executors.shutdown();
    }

    @Override
    public void run() {
        while (num.get() < MAX_VALUE) {
            try {
                printHelloWorld(MAX_VALUE, num);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
